<?php
class Crawl_html_by_url_library  extends Crawl_html_library{

    public  $_platform;
    public  $_htmlsearch_config;

    /**
     * 定义 用户端信息、抓取地址
     * 定义 新浪网页抓取选项
     */
    function __construct(){
        parent::__construct();
        $this->write_log_stary_tag();
        $this->_get_parameters = $this->_ci->input->get_post(NULL,TRUE);
    }


    public function crawl($retry = FALSE) {

        $crawl_result = $this->try_crawl($retry);
        if(is_array($crawl_result) && array_key_exists($this->_error_string, $crawl_result)) {
            return $crawl_result;
        } else {
            return $crawl_result;
        }
    }

    /**
     * 尝试抓取
     * @param bool $retry 是否重试
     * @return array|bool 抓取成功返回数组
     */
    private function try_crawl() {
        $parameters = $this->make_parameters();
        if(isset($this->_parameters['use_proxy']) && $this->_parameters['use_proxy'] === TRUE) {
            $crawl_use_proxy_ip_result = $this->crawl_use_proxy_ip($this->_parameters, self::PRROXY_IP, $this->_crawl_from['user_check']);
            if(is_array($crawl_use_proxy_ip_result) && array_key_exists($this->_error_string, $crawl_use_proxy_ip_result)) {
                return $this->crawl_use_proxy_ip($this->_parameters, self::MOBILITY_IP, $this->_crawl_from['user_check']);
            } else {
                return $crawl_use_proxy_ip_result;
            }
        } else {
            //不使用代理、不重试直接抓取
            log_message(self::LOG_LEVEL_NOTICE, 'Will Crawl From Other Web Site Un Use Retry And Un Use Proxy With Parameters: '.json_encode($parameters), FALSE, $this->_log_dir);
            return self::get_content($parameters);

        }
    }

    public function process_crawl_result($crawl_result, $use_proxy = self::PRROXY_IP) {
        if(is_array($crawl_result) && array_key_exists($this->_error_string, $crawl_result)) {
            return $crawl_result;
        } else {
            return $crawl_result['result'];
        }
    }

    private function make_parameters() {
        $parameters['url'] = iconv("GBK", "UTF-8//TRANSLIT//IGNORE", $this->_get_parameters['url']);
        if(isset($this->_get_parameters['cookie'])) {
            $parameters['cookie_str'] = true;
            $parameters['cookie'] = urldecode($this->_get_parameters['cookie']);
        }
        if(isset($this->_get_parameters['proxy']) && $this->_get_parameters['proxy'] == self::USE_PROXY) {
            $parameters['use_proxy'] = TRUE;
            unset($parameters['proxy']);
        }
        if(isset($this->_get_parameters['timeout'])) {
            $parameters['time_out'] = intval($this->_get_parameters['timeout']);
        }
        return $this->_parameters = $parameters;
    }

}